<template>
  <el-dialog :title="formData.id? '编辑部门':'新增部门'" :visible="isShowDialog" @close="closeBtn">
    <!-- 新增部门的弹层 -->
    <!-- 表单组件  el-form  label-width设置label的宽度  -->
    <!-- 匿名插槽 -->
    <el-form ref="addDepts" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="user in EmployeeList" :key="user.id" :value="user.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="closeBtn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { getDeptsDetail, getDeptsList } from '@/api/department'
import { addDepts, editDepts } from '@/api/department'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }

  },

  data() {
    const checkRepeatCode = async(rule, val, callback) => {
      // 1.校验
      const { depts } = await getDeptsList()
      console.log(53, depts)

      // 区分状态
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === val && item.id !== this.formData.id)
      } else {
        isRepeat = depts.some(item => item.code === val)
      }

      if (isRepeat) {
        // 失败
        callback(new Error('全公司部门编码必须唯一'))
      } else {
        // 成功
        callback()
      }
    }
    const checkRepeatName = async(rule, val, callback) => {
      // 1.校验
      const { depts } = await getDeptsList()
      console.log(53, depts)
      // 区分状态
      let isRepeat
      if (this.formData.id) {
        // 编辑(找兄弟)
        // 先找出同一个部门,如果不是同一个部门重名那就无所谓,然后再找到编码相同id不同
        isRepeat = depts.filter(item => item.pid === this.formData.pid).some(item => item.code === val && item.id !== this.formData.id)
      } else {
        // 新增(找儿子)
        // 先找出同一个部门下的所有子部门,然后再找到编码相同id不同
        isRepeat = depts.filter(item => item.pid === this.formData.id).some(item => item.name === val)
      }

      if (isRepeat) {
        callback(new Error('全公司部门编码必须唯一'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatName, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      EmployeeList: []// 部门的列表
    }
  },

  methods: {
    async getEmployeeSimple() {
      this.EmployeeList = await getEmployeeSimple()
    //   console.log(61, this.EmployeeList)
    },
    async getDeptsDetail(id) {
      console.log(64)
      this.formData = await getDeptsDetail(id)
    },
    async btnOk() {
      if (this.formData.id) {
        await editDepts(this.formData)
      } else {
        await addDepts({
          ...this.formData,
          pid: this.nodeData.id
        })
      }
      this.$message.success('操作成功')
      this.$emit('update:isShowDialog', false)
      this.$emit('reloadPage')
    },
    async closeBtn() {
      // 清空数据
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清理表单报错
      this.$refs.addDepts.resetFields()
      // 关闭弹窗
      this.$emit('update:isShowDialog', false)
    }
  }

}
</script>
<style lang="">

</style>
